// ignore_for_file: slash_for_doc_comments, unnecessary_brace_in_string_interps, avoid_print, unused_import, duplicate_import

import 'dart:async';
import 'dart:typed_data';
import 'dart:core';
import 'package:flutter/services.dart';

///抽象类
import './base/abstract/IFFRefresh.dart';
import './base/abstract/IFFScaffold.dart';

///配置类
import './base/config/DeaultPageConfig.dart';

///demos
import './base/demos/FFHeadListPage.dart';
import './base/demos/FFListPage.dart';
import './base/demos/FFPage.dart';
import './base/demos/Start.dart';

///基类
import './base/super/FFGestureDetector.dart';
import './base/super/FFState.dart';
import './base/super/FFStatefulWidget.dart';

///tool
import './base/tool/arouter/FFGoto.dart';
import './base/tool/empty/FFEmpty.dart';
import './base/tool/empty/FFEmpty.dart';
import './base/tool/extension/FFListExt.dart';
import './base/tool/extension/FFMapExt.dart';


///widget 布局基础类
import './base/widget/layout/FFNavi.dart';
import './base/widget/layout/FFTextButton.dart';
import './base/widget/layout/FFImageButton.dart';
import './base/widget/layout/FFLineContainer.dart';
import './base/widget/layout/FFStackContainer.dart';
import './base/widget/layout/FFBranch.dart';
import './base/widget/layout/FFMoreBrach.dart';
import './base/widget/layout/FFSize.dart';

///page
import './base/widget/page/FFListScaffold.dart';
import './base/widget/page/FFScaffold.dart';

///widgetTool
import './base/widgetTool/image/FFImageUtil.dart';



class YcPluginLearn {
  /**
   * 通道MethodChannel，用于flutter给原生传值，与原生传入相同的字符串
   */
  static const MethodChannel _channel = MethodChannel('yc_plugin_learn');

  /**
   * EventChannel，用于原生给flutter传值
   */
  static const EventChannel _eventChannel =
      EventChannel("yc_plugin_learn_eventChannel");

  /**
   * 接口，配合EventChannel _eventChannel： 用于原生给flutter传值，面向接口编程
   */
  static late YcPluginCallBack _callBack;

  /**
   * 初始化接口
   */
  static set ycPluginCallBack(YcPluginCallBack value) {
    _callBack = value;
  }

  /**
   * 初始化这个sdk
   */
  static Future<dynamic> init({
    required String appId,
  }) async {
    eventChannel();

    //todo:其他事情
    return await _channel.invokeMethod("init", {"appId": appId});
  }

  /**
   * EventChannel，监听原生的调用
   */
  static eventChannel() {
    _eventChannel.receiveBroadcastStream().listen((data) {
      String type = data["type"];
      switch (type) {
        case "0":
          _callBack.secondCallBack(data['second']);
          break;
        case "1":
          _callBack.minuteCallBack(data['minute']);
          break;
        case "2":
          _callBack.appListCallBack(data['list']);
          break;
        default:
          print("YcPluginLearn eventChannel listen = ${data}");
      }
    });
  }

  /**
   * platformVersion get方法获取平台版本
   */
  static Future<String?> get platformVersion async {
    final String? version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  /**
   * 安装apk
   */
  static Future<void> installApp(String savePath, String appId) async {
    await _channel
        .invokeMethod('installApp', {"file": savePath, 'appId': appId});
  }

  /**
   * 获取外部储存位置 /storage/emulated/0
   */
  static Future<String> storagePath() async {
    final String? path = await _channel.invokeMethod('storagePath');
    return path ?? "";
  }

  /**
   * 启动apk
   */
  static Future<bool> launchApp(String packageName) async {
    try {
      await _channel.invokeMethod('launchApp', {"packageName": packageName});
      return true;
    } catch (e) {
      print("启动apk error = $e");
      return false;
    }
    return false;
  }

  /**
   * 卸载apk
   */
  static Future<void> unInstallApp(String baoName) async {
    try {
      await _channel.invokeMethod('unInstallApp', {"packageName": baoName});
      print("CPS手游,去提现用户卸载33333");
    } catch (e) {
      print("卸载apk error = $e");
    }
  }

  /**
   * 获取应用列表
   */
  static Future<dynamic> appList() async {
    return _channel.invokeMethod("appList", {});
  }

  /**
   * 原生去请求图片，并返回图片数据
   */
  static Future<Uint8List> getImage(String url) async {
    //发一个"getImage"消息给原生，并传入参数
    return await _channel.invokeMethod("getImage", {'url': url});
  }

  /**
   * 测试，用于触发secondCallBack
   */
  static void test() {
    _channel.invokeListMethod("test");
  }
}

/**
 * 抽象类
 */
abstract class YcPluginCallBack {
  //自定义需要的方法
  void secondCallBack(String second) {}

  //自定义需要的方法
  void minuteCallBack(String minute) {}

  //回调原生得到的应用列表
  void appListCallBack(dynamic list) {}
}
